package com.vsd.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.annotation.LoginFilter;
import com.demo.bean.DataTable;
import com.demo.bean.Ret;
import com.demo.controller.CoreAdminController;
import com.demo.dialet.PaginationSupport;
import com.demo.util.JSONUtils;
import com.demo.validator.AdminLoginValidate;
import com.shopcart.domain.QuickShopping;
import com.shopcart.domain.StoreGoods;
import com.shopcart.service.QuickShoppingService;
import com.vsd.daoEx.model.QuickShoppingEx;
import com.vsd.service.StoreGoodsService;

@Controller
@RequestMapping("quickShopping")
@LoginFilter(validateClz=AdminLoginValidate.class)
public class QuickShoppingController extends CoreAdminController{
	@Autowired
	private QuickShoppingService quickShoppingService;
	@Autowired
	private StoreGoodsService storeGoodsService;
	
	@RequestMapping("/list")
	@ResponseBody
	public String citylist(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String areaCode,HttpServletRequest request){
		
		PaginationSupport.byPage(start/length+1, length);
		List<QuickShoppingEx> list = quickShoppingService.selectExList(areaCode);
		
		return JSONUtils.serialize(new DataTable(draw+1,PaginationSupport.getContext().getTotalCount(), list));
	}
	
	@RequestMapping("/listByStore")
	@ResponseBody
	public String listByStore(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String areaCode,HttpServletRequest request){
		String storeId = getSessionParameter("id", session);
		PaginationSupport.byPage(start/length+1, length);
		List<QuickShoppingEx> list = quickShoppingService.selectByStore(storeId);
		
		return JSONUtils.serialize(new DataTable(draw+1,PaginationSupport.getContext().getTotalCount(), list));
	}
  
	
	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String string : ids) {
			quickShoppingService.delById(string);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(QuickShopping s,HttpSession session){
		StoreGoods g = storeGoodsService.findById(s.getGid());
		if(g==null||!g.getStoreId().equals(getSessionParameter("id", session))) return JSONUtils.serialize(new Ret(-1,"商品未找到"));
		
		if(g.getType()!=1){
			 return JSONUtils.serialize(new Ret(-1,"仅团购商品可加入抢购"));
		}
		QuickShopping qss = quickShoppingService.findById(s.getGid());
		if(qss!=null&&qss.getState()==3&&qss.getBeginTime().compareTo(new Date())>0) return JSONUtils.serialize(new Ret(-1,"该商品已在抢购中"));
		
		if(qss==null){
			s.setHasSn(0);
			s.setState(1);
			s.setTotal(s.getAllCnt());
			s.setCreateTime(new Date());
			quickShoppingService.save(s);
		}else{
			s.setState(1);
			s.setTotal(s.getAllCnt());
			quickShoppingService.updateByPrimaryKey(s);
		}
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public String update(QuickShopping s){
		quickShoppingService.updateByPrimaryKey(s);
		return JSONUtils.serialize(new Ret(0));
	}
	
}
